package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<SetmealVO> list = setmealMapper.page(dto);
        Page page = (Page) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void add(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);

        setmealMapper.add(setmeal);
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.addBatch(setmealDishes);
    }

    @Transactional
    @Override
    public void deleteBatch(Integer[] ids) {
        for (Integer id : ids) {
            Integer status = setmealMapper.getStatus(id);
            if (status == 1) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.deleteBatch(ids);
        setmealDishMapper.deleteBatch(ids);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }

    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO sv = setmealMapper.getById(id);
        List<SetmealDish> list = setmealDishMapper.selectBySetmealId(id);
        sv.setSetmealDishes(list);
        return sv;
    }

    @Override
    @Transactional
    public void update(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.update(setmeal);
        Long setmealId = dto.getId();
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        if(setmealDishes!=null&&setmealDishes.size()>0){
        setmealDishMapper.deleteById(setmealId);
        }
        setmealDishMapper.addBatch(setmealDishes);
    }

    @Override
    public List<Setmeal> selectByCategoryId(Long id) {
       List<Setmeal> setmeals =  setmealMapper.getByCategoryId(id);
       return setmeals;
    }

}
